package com.c2.web.backend;

import com.c2.entity.EbNews;
import com.c2.entity.EbProduct;
import com.c2.entity.EbProductCategory;
import com.c2.service.NewsService;
import com.c2.service.ProductCategoryService;
import com.c2.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Stars Lee
 * @date 2020/9/16 - 9:51
 */

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    NewsService newsService;
    @Autowired
    ProductService productService;
    @Autowired
    ProductCategoryService productCategoryService;
    @RequestMapping("/manage")
    public String manage(){
        return "manage/index";
    }
    @RequestMapping("/front")
    public String front(Model model,
                        @RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        List<EbProduct> frontProduct=productService.listByPage(page,pageSize);
        Integer maxPage=productService.findMaxPage(pageSize);
        List<EbProductCategory> bigClassList=productCategoryService.bigClassList();
        List<EbNews> ebNewsList = newsService.listByPage(1, 10);
        model.addAttribute("frontProduct",frontProduct);
        model.addAttribute("maxPage",maxPage);
        model.addAttribute("currentPage",page);
        model.addAttribute("ebNewsList",ebNewsList);
        model.addAttribute("bigClassList",bigClassList);


//        for (int i=0;i<bigClassList.size();i++){
//            Integer parentId=bigClassList.get(i).getEpcId();
//            List<EbProductCategory> smallClassList=productCategoryService.smallClassList(parentId);
//            ListProductCategory.add(smallClassList);
//        }
//        model.addAttribute("ListProductCategory",ListProductCategory);
        return "index";
    }

    @RequestMapping("/productView")
    public String productView(Model model,
                              @RequestParam(value = "parentId") Integer parentId,
                              @RequestParam(value = "epId")Integer epId,
                              @RequestParam(value = "epName")String epName,
                              @RequestParam(value = "epDescription")String epDescription,
                              @RequestParam(value = "epPrice")Double epPrice,
                              @RequestParam(value = "epStock")Integer epStock,
                              @RequestParam(value = "epFileName")String epFileName){
        model.addAttribute("epId",epId);
        model.addAttribute("epName",epName);
        model.addAttribute("epDescription",epDescription);
        model.addAttribute("epPrice",epPrice);
        model.addAttribute("epStock",epStock);
        model.addAttribute("epFileName",epFileName);

        List<EbProductCategory> bigClassList=productCategoryService.bigClassList();
        model.addAttribute("bigClassList",bigClassList);
        List<EbProductCategory> smallClassList=productCategoryService.smallClassList(parentId);
        model.addAttribute("smallClassList",smallClassList);

        return "product-view";
    }

    @RequestMapping("/likeList")//模糊查询
    public String likeList(Model model , String inEpName,
                           @RequestParam(value = "likePage",defaultValue = "1") Integer likePage,
                           @RequestParam(value = "likePageSize",defaultValue = "2") Integer likePageSize,
                           @RequestParam(value = "parentId",defaultValue = "1") Integer parentId){
        List<EbProduct> likeList=productService.likeByPage(inEpName,likePage,likePageSize);
        model.addAttribute("likeList",likeList);
        Integer likeMaxPage=productService.findLikeMaxPage(inEpName,likePageSize);
        model.addAttribute("likeMaxPage",likeMaxPage);
        Integer likeCurrentPage=likePage;
        model.addAttribute("likeCurrentPage",likeCurrentPage);

        List<EbProductCategory> bigClassList=productCategoryService.bigClassList();
        model.addAttribute("bigClassList",bigClassList);
        List<EbProductCategory> smallClassList=productCategoryService.smallClassList(parentId);
        model.addAttribute("smallClassList",smallClassList);
        model.addAttribute("inEpName",inEpName);
        model.addAttribute("parentId",parentId);

        return "product-list";
    }

//    @RequestMapping("/bigCategoryList")//查询大类
//    public String bigCategoryList(Model model){
//        List<EbProductCategory> bigClassList=productCategoryService.bigClassList();
//        model.addAttribute("bigClassList",bigClassList);
//        return "index";
//    }

    @RequestMapping("/smallCategoryList")//查询小类
    public String smallCategoryList(Model model,
                                    @RequestParam(value = "parentId") Integer parentId,
                                    @RequestParam(value = "page",defaultValue = "1")Integer page,
                                    @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        List<EbProduct> frontProduct=productService.listByPage(page,pageSize);
        model.addAttribute("frontProduct",frontProduct);
        Integer maxPage=productService.findMaxPage(pageSize);
        model.addAttribute("maxPage",maxPage);
        Integer currentPage=page;
        model.addAttribute("currentPage",currentPage);
        List<EbProductCategory> bigClassList=productCategoryService.bigClassList();
        model.addAttribute("bigClassList",bigClassList);
        List<EbProductCategory> smallClassList=productCategoryService.smallClassList(parentId);
        model.addAttribute("smallClassList",smallClassList);
        return "index";
    }
}

